﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmdonthuoc : Form
    {
        public string _maBN;
        public string _tenBN;
        static long _dongia = 0;
        public frmdonthuoc()
        {
            InitializeComponent();
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

            TV_Tonghop.Themcot("STT", "STT", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Hoten", "Bệnh nhân", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Tenthuoc", "Tên thuốc", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Soluong", "Số lựong", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("DUONG_DUNG", "Đường Dùng", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Dongia", "Tổng tiền", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Ngayratoa", "Ngày ra toa", dt_donthuoc, DataGridViewAutoSizeColumnMode.Fill);
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

        private void frmdonthuoc_Load(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            DataTable dtb = new DataTable();
            DataTable dtb1 = new DataTable();

            SqlCommand cmd = new SqlCommand(@"  Select a.STT, c.HOTEN_BN as Hoten, b.TEN_LOAI as Tenthuoc, a.Soluong,a.Duong_dung,a.Dongia,a.Ngayratoa
                                                From DON_THUOC a join LOAI_THUOC b on a.ThuocID=b.MA_LOAI
				                                                 join BENH_NHAN c on a.BenhnhanID=c.MA_BN
                                                Where a.BenhnhanID = '"+ _maBN +"' ", ketnoi);
            dtb.Load(cmd.ExecuteReader());
            dt_donthuoc.DataSource = dtb;

            SqlCommand cmd1 = new SqlCommand(@" Select * From Loai_Thuoc ", ketnoi);
            dtb1.Load(cmd1.ExecuteReader());
            cboTenthuoc.DataSource = dtb1;

            txtBenhnhan.Text = _tenBN;
            cboTenthuoc.Focus();
            txtNgayratoa.Text = DateTime.Now + "";
        }

        private void bt_Them_Click(object sender, EventArgs e)
        {
            string _thuoc = cboTenthuoc.SelectedValue + "";
            int _soluong = Convert.ToInt32(txtSoluong.Text);
            DateTime _ngayratoa = DateTime.Now;
            string _duongDung = txtDuongdung.Text;
            long _gia = _soluong * _dongia;
            
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand(@"Insert into DON_THUOC(ThuocID,Soluong,Ngayratoa,Duong_dung,Dongia,BenhnhanID)
                                              Values ('"+_thuoc+"', '"+_soluong+"', '"+_ngayratoa+"', N'"+_duongDung+"', '"+_gia+"','"+_maBN+"' ) ", ketnoi);
            int a = cmd.ExecuteNonQuery();
            if (a == 1)
            {
                MessageBox.Show("Thêm thành công", "Thông báo");
                frmdonthuoc_Load(sender, e);
            }



        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            //SqlConnection ketnoi = getconnect();
            //ketnoi.Open();
            //SqlCommand cmd = new SqlCommand("CapNhatDonThuoc", ketnoi);
            //cmd.CommandType = CommandType.StoredProcedure;

            //cmd.Parameters.Add("@ma_dthuoc", SqlDbType.NChar).Value = txb_madonthuoc.Text;
            //cmd.Parameters.Add("@stt_phieu", SqlDbType.Int).Value = Convert.ToInt32(txb_sttphieu.Text);
            //cmd.Parameters.Add("@ma_loai", SqlDbType.NChar).Value = txtBenhnhan.Text;
            //cmd.Parameters.Add("@ten_thuoc", SqlDbType.NVarChar).Value = cboTenthuoc.Text;
            //cmd.Parameters.Add("@soluong_thuoc", SqlDbType.Int).Value = txtSoluong.Text;
            //cmd.Parameters.Add("@duong_dung", SqlDbType.Int).Value = cboDuongdung.Text;

            //cmd.ExecuteNonQuery();
            //frmdonthuoc_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
        //    SqlConnection ketnoi = getconnect();
        //    ketnoi.Open();
        //    SqlCommand cmd = new SqlCommand("XoaDonThuoc", ketnoi);
        //    cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
        //    cmd.Parameters.Add("@ma_dthuoc", SqlDbType.Char).Value = txb_madonthuoc.Text;
        //    cmd.ExecuteNonQuery();
        //    frmdonthuoc_Load(sender, e);
        }


        //int dong;
        private void dt_donthuoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
        //    dong = e.RowIndex;
            //txb_madonthuoc.Text = dt_donthuoc.Rows[dong].Cells["Ma_DThuoc"].Value.ToString();
            //txb_sttphieu.Text = dt_donthuoc.Rows[dong].Cells["STT_Phieu"].Value.ToString();

            //txtBenhnhan.Text = dt_donthuoc.Rows[dong].Cells["Ma_Loai"].Value.ToString();
            //cboTenthuoc.Text = dt_donthuoc.Rows[dong].Cells["Ten_Thuoc"].Value.ToString();
            //txtSoluong.Text = dt_donthuoc.Rows[dong].Cells["So_Luong"].Value.ToString();
            //cboDuongdung.Text = dt_donthuoc.Rows[dong].Cells["Duong_Dung"].Value.ToString();
        }

        private void cboTenthuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            string _mathuoc = cboTenthuoc.SelectedValue + "";

            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand(@"  Select Gia From Loai_Thuoc
                                                Where Ma_Loai = '" + _mathuoc + "' ", ketnoi);
            DataTable dtb = new DataTable();
            dtb.Load(cmd.ExecuteReader());
            _dongia = Convert.ToInt64(dtb.Rows[0]["Gia"]);

            txtDongia.Text = _dongia + "";
        }

    }
}
